package dingtalk

import (
	"fmt"

	"gitee.com/lorock/go-dingtalk-sdk/dingtalkhttplib"

	jsoniter "github.com/json-iterator/go"
	log "github.com/sirupsen/logrus"
)

// DepartmentCreate 创建部门
func (c *Client) DepartmentCreate(department *CreateDepartment) (*DepartmentCreateRsp, error) {
	rsp := new(DepartmentCreateRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	jsonParms, err := json.Marshal(department)
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", DepartmentCreate, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Post(url).Body(jsonParms).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentUpdate 更新部门
func (c *Client) DepartmentUpdate(department *UpdateDepartment) (*DepartmentCreateRsp, error) {
	rsp := new(DepartmentCreateRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	jsonParms, err := json.Marshal(department)
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s", DepartmentUpdate, accessToken)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Post(url).Body(jsonParms).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentDelete 删除部门
func (c *Client) DepartmentDelete(departmentid int) (*DepartmentCreateRsp, error) {
	rsp := new(DepartmentCreateRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&id=%d", DepartmentDelete, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentListIds 获取子部门ID列表
func (c *Client) DepartmentListIds(departmentid int) (*DepartmentListIdsRsp, error) {
	rsp := new(DepartmentListIdsRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&id=%d", DepartmentListIds, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentList 获取部门列表
func (c *Client) DepartmentList(departmentid int) (*DepartmentListRsp, error) {
	rsp := new(DepartmentListRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&id=%d", DepartmentList, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentGet 获取部门详情
func (c *Client) DepartmentGet(departmentid int) (*DepartmentGetRsp, error) {
	rsp := new(DepartmentGetRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&id=%d", DepartmentGet, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}

// DepartmentListParent 查询部门的所有上级父部门路径
func (c *Client) DepartmentListParent(departmentid int) (*DepartmentListParentRsp, error) {
	rsp := new(DepartmentListParentRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}

	url := fmt.Sprintf("%s?access_token=%s&id=%d", DepartmentListParent, accessToken, departmentid)
	if c.Debug {
		log.Println("url:", url)
	}
	body, err := dingtalkhttplib.Get(url).AsBytes()
	if err != nil {
		return rsp, err
	}
	err = json.Unmarshal(body, rsp)
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}
